<?php
declare (strict_types = 1);

namespace app\index\model\charge;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class ChargePileCost extends Model
{
    protected $pk = "id";

    /**
     * 根据id获取对应的信息
     * @param $costId
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($costId){
        return $this->where('id',$costId)->find();
    }

    /**
     * 获取指定充电桩当前的费用
     * @param $pileInfo
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getPileChargeCostInfo($pileInfo){
        $costInfo = $this->getInfoById($pileInfo['cost_id']);
        if(!$costInfo) return 0;
        $costList = json_decode($costInfo['cost_value'],true);
        $getHouse = date("H");
        $getMin = date("i");
        $getKey = $getHouse * 2;
        if($getMin > 30) $getKey = $getKey + 1;
        if(isset($costList[$getKey])){
            $getCostValue = $costList[$getKey]['value'];
        }else{
            $getCostValue = "01";
        }
        //尖费率
        if($getCostValue == "00" || $getCostValue == 0){
            return number_format(($pileInfo['charge_cost'] + $pileInfo['service_cost']),2);
            //峰费率
        } else if($getCostValue == "01" || $getCostValue == 1){
            return number_format(($pileInfo['peak_electric_cost'] + $pileInfo['peak_service_cost']),2);
            //平费率
        }else if($getCostValue == "02" || $getCostValue == 2){
            return number_format(($pileInfo['flat_electric_cost'] + $pileInfo['flat_service_cost']),2);
            //谷费率
        }else if($getCostValue == "03" || $getCostValue == 3){
            return number_format(($pileInfo['valley_electric_cost'] + $pileInfo['valley_service_cost']),2);
        }else{
            return 0;
        }
    }


}
